package io.renren.modules.report.service.impl;

import io.renren.modules.report.entity.ConnectUserEntity;
import io.renren.modules.report.service.ConnectUserService;
import io.renren.modules.report.vo.ConnectionUserVO;
import io.renren.modules.report.vo.UserVO;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.report.dao.ConnectionDao;
import io.renren.modules.report.entity.ConnectionEntity;
import io.renren.modules.report.service.ConnectionService;
import org.springframework.transaction.annotation.Transactional;


@Service("connectionService")
public class ConnectionServiceImpl extends ServiceImpl<ConnectionDao, ConnectionEntity> implements ConnectionService {

    @Autowired
    private ConnectUserService connectUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String name = (String)params.get("name");

        IPage<ConnectionEntity> page = this.page(
                new Query<ConnectionEntity>().getPage(params),
                new QueryWrapper<ConnectionEntity>().like(StringUtils.isNotBlank(name),"name", name)
        );

        return new PageUtils(page);
    }

    @Override
    public List<UserVO> getUsersbyConnectionID(Long connectId) {
        return this.baseMapper.getUsersbyConnectionID(connectId);
    }

    @Transactional
    public void saveCascade(ConnectionUserVO connectionUserVO) {

        ConnectionEntity connectionEntity = new ConnectionEntity();
        BeanUtils.copyProperties(connectionUserVO,connectionEntity);

        connectionEntity.setCreateDate(new Date());
        if(connectionEntity.getDriverName().equals("1")){
            connectionEntity.setDriverName("mysql");
        }
        connectionEntity.setCreateDate(new Date());
        this.save(connectionEntity);

        connectionUserVO.getUserIds().stream().forEach(item->{
            ConnectUserEntity connectUserEntity = new ConnectUserEntity();
            connectUserEntity.setUserId(item);
            connectUserEntity.setConnectId(connectionEntity.getId());
            connectUserService.save(connectUserEntity);
        });



    }

    /**
     * 1 更新connect表
     * 2 更新关联关系表conectuser表
     *    2.1 先删除
     *    2.2 在增加
     * @param connectionUserVO
     */
    @Override
    @Transactional
    public void updateCascade(ConnectionUserVO connectionUserVO) {
        ConnectionEntity connectionEntity = new ConnectionEntity();
        BeanUtils.copyProperties(connectionUserVO,connectionEntity);

        connectionEntity.setCreateDate(new Date());
        if(connectionEntity.getDriverName().equals("1")){
            connectionEntity.setDriverName("mysql");
        }
        this.updateById(connectionEntity);
        connectUserService.remove(new QueryWrapper<ConnectUserEntity>().eq("connect_id",connectionEntity.getId()));
        connectionUserVO.getUserIds().stream().forEach(item->{
            ConnectUserEntity connectUserEntity = new ConnectUserEntity();
            connectUserEntity.setUserId(item);
            connectUserEntity.setConnectId(connectionEntity.getId());
            connectUserService.save(connectUserEntity);
        });

    }

    @Override
    public void deleteCascade(Long[] ids) {
        List<Long> idsList = Arrays.asList(ids);
        this.baseMapper.deleteBatchIds(idsList);

        idsList.stream().forEach(item->{
            connectUserService.remove(new QueryWrapper<ConnectUserEntity>().eq("connect_id",item));
        });
    }

}